package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ThirdPartPayTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.paymenthub.IChinaPayTransferAccountServiceClient;
import com.itlong.cloud.paymenthub.IWeChatPayServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


/**
 * <desc>
 * 运营-银联转账控制器.
 * </desc>
 *
 * @createDate 2018/9/1
 */

@RestController
@RequestMapping("/finance")
public class ChinaPayTransferAccountController {

    private static final Logger LOG = LoggerFactory.getLogger(ChinaPayTransferAccountController.class);

    @Autowired
    IChinaPayTransferAccountServiceClient iChinaPayTransferAccountServiceClient;

    @Autowired
    IWeChatPayServiceClient wechatPayServiceClient;

    /**
     * <desc>
     *      账务审核 根据提现记录ID向物业公司转账
     * <desc/>
     *
     * @param cashIds 提现记录ID,逗号分隔
     * @return
     * @author Juguang.S
     * @createDate 2018-09-01
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "/withdraw/pass")
    @LoggerInfo(operateDesc = LogDescConstant.WITHDRAW_PASS_DESC)
    public Object transferAccount(String cashIds,String myUserName){
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cashIds=request.getAttribute("cashIds")==null?null:request.getAttribute("cashIds").toString();
            myUserName=request.getAttribute("myUserName")==null?null:request.getAttribute("myUserName").toString();
            if(StringUtils.isEmpty(cashIds) || StringUtils.isEmpty(myUserName)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            //校验第三方支付管理银联转账是否被禁用
            if(wechatPayServiceClient.getThirdPayStatus(ThirdPartPayTypeEnum.FACE_PAY.getType())==0){
                return new MessageVO(OperateErrorCodeEnum.NOT_ALLOWED_CHINAPAY_TRADE.getErrorCode());
            }

            //查询银联转账的手续费
            String handFee = iChinaPayTransferAccountServiceClient.getChinaPayHandFee();
            if(StringUtils.isEmpty(handFee)){
                handFee = "2";
            }

            //校验所转账的总金额是否会大于备付金的金额
//            String amount = iChinaPayTransferAccountServiceClient.getTotal(cashIds);
//            String accountBalance = iChinaPayTransferAccountServiceClient.getAccountBalance();
//            if(Double.parseDouble(new BigDecimal(amount).add(new BigDecimal(handFee)).toString())>Double.parseDouble(accountBalance)){
//                return new MessageVO(OperateErrorCodeEnum.AMCOUNT_NOT_ENOUGH_ERR.getErrorCode());
//            }
            iChinaPayTransferAccountServiceClient.transferAccount(cashIds,myUserName);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_根据提现记录ID向物业公司转账-transferAccount】失败",e);
        }
    }

}
